/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ant.antsys;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Define test case in the system test
 * @author johnson ma
 */
public class TestCase extends TestTask {
	public static final String TESTCASE_TO_RUN = "testcase";
	private String name;
	//if skip euqals true, this testcase won't be executed
	private boolean skip = false;
	//propagate from TestSuite attribute
	private boolean failOnError = false;

	// The method executing the task
	public void execute() throws BuildException {
		if (name == null) {
	            throw new BuildException("The name attribute must be present.",
				    getLocation());
	        }
		//check if user spacified the test case name to run
		String testCaseToRun = getProject().getProperty(TESTCASE_TO_RUN);
		if (testCaseToRun != null && !testCaseToRun.equals(name)) {
		    return;
		}
                //check if condition 
                if (!checkCondition()) {
                    return;
                }

		if(!skip){
			notifyCaseStart(this);
			for (Task t : tasks) {
				try {
					t.perform();
				} catch (BuildException e) {
					if (failOnError) {
						throw e;
					}
					//check if it is assert 
					notifyCaseFail(this, e.toString(), t.getLocation().toString());
					//becaues of the failure, won't need to continue this test case
					break;
				}
			}
			notifyCaseFinish(this);
		}
	}

	// The setter for the "name" attribute
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	// The setter for the "skip" attribute
	public void setSkip(boolean name) {
		this.skip = name;
	}

	public boolean getSkip(){
		return skip;
	}
	public void setFailOnError(boolean failOnError) {
		this.failOnError = failOnError;
	}

}


